﻿using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace isky.Plugin
{
   public class CommonWcf
    {
        public CommonWcf()
        {
        }

        internal void GetAttributeInputOrImage<T>(string v1, int v2)
        {
            throw new NotImplementedException();
        }

        ///// <summary>
        ///// 批量创建
        ///// </summary>
        ///// <param name="entities"></param>
        ///// <returns></returns>
        //public ExecuteMultipleResponse ExecultMutipleCreate(IEnumerable<Entity> entities)
        //{
        //    //res = string.Empty;
        //    ExecuteMultipleRequest requestWithResults = null;
        //    requestWithResults = new ExecuteMultipleRequest()
        //    {
        //        // Assign settings that define execution behavior: continue on error, return responses. 
        //        Settings = new ExecuteMultipleSettings()
        //        {
        //            ContinueOnError = false,
        //            ReturnResponses = true
        //        },
        //        // Create an empty organization request collection.
        //        Requests = new OrganizationRequestCollection()
        //    };

        //    // Add a CreateRequest for each entity to the request collection.
        //    foreach (var entity in entities)
        //    {
        //        CreateRequest createRequest = new CreateRequest { Target = entity };
        //        requestWithResults.Requests.Add(createRequest);
        //    }

        //    ExecuteMultipleResponse response = (ExecuteMultipleResponse)xrmService.GetService().Execute(requestWithResults);
        //    return response;
        //}

        /// <summary>
        /// 批量更新
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public ExecuteMultipleResponse ExecultMutipleUpdate(IEnumerable<Entity> entities, IOrganizationService service)
        {
            //res = string.Empty;
            ExecuteMultipleRequest requestWithResults = null;
            requestWithResults = new ExecuteMultipleRequest()
            {
                // Assign settings that define execution behavior: continue on error, return responses. 
                Settings = new ExecuteMultipleSettings()
                {
                    ContinueOnError = false,
                    ReturnResponses = true
                },
                // Create an empty organization request collection.
                Requests = new OrganizationRequestCollection()
            };

            // Add a CreateRequest for each entity to the request collection.
            foreach (var entity in entities)
            {
                UpdateRequest updateRequest = new UpdateRequest { Target = entity };
                requestWithResults.Requests.Add(updateRequest);
            }

            ExecuteMultipleResponse response = (ExecuteMultipleResponse)service.Execute(requestWithResults);
            return response;
        }

        /// <summary>
        /// 批量创建
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public ExecuteMultipleResponse ExecultMutipleCreate(IEnumerable<Entity> entities, IOrganizationService service)
        {
            //res = string.Empty;
            ExecuteMultipleRequest requestWithResults = null;
            requestWithResults = new ExecuteMultipleRequest()
            {
                // Assign settings that define execution behavior: continue on error, return responses. 
                Settings = new ExecuteMultipleSettings()
                {
                    ContinueOnError = false,
                    ReturnResponses = true
                },
                // Create an empty organization request collection.
                Requests = new OrganizationRequestCollection()
            };

            // Add a CreateRequest for each entity to the request collection.
            foreach (var entity in entities)
            {
                CreateRequest createRequest = new CreateRequest { Target = entity };
                requestWithResults.Requests.Add(createRequest);
            }

            ExecuteMultipleResponse response = (ExecuteMultipleResponse)service.Execute(requestWithResults);
            return response;
        }


        public T GetAttributeInputOrImage<T>(Entity preEntity, Entity PreImageEntity, string attrName, T defaultValue)
        {
            if (preEntity.Attributes.ContainsKey(attrName))
            {
                return preEntity.GetAttributeDefaultValue<T>(attrName, defaultValue);
            }
            else
            {
                if (PreImageEntity==null)
                {
                    return defaultValue;
                }
                else
                {
                    return PreImageEntity.GetAttributeDefaultValue<T>(attrName, defaultValue);
                }
                
            }
        }
        

    }
}
